<template>
  <BaseDialog  :loading="loading" destroy-on-close @confirm="confirm" @close="close" confirmText="确定" v-model="isShow" title="选择模板">
    <el-form
        :model="queryForm"
    >
      <el-form-item prop="templateName" label="模板名称">
        <el-input type="text" v-model="queryForm.templateName" placeholder="请输入模板名称"/>
      </el-form-item>
    </el-form>
    <el-table ref="tableRef" height="100%" v-loading="loading" :data="data">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="60" align="center" header-align="center"/>
      <el-table-column prop="templateName" label="模板名称"/>
      <el-table-column prop="templateType" label="模板类型" align="center" header-align="center" width="180">
        <template #default="scope">
          <BaseTag :value="scope.row.templateType" :dict="templateTypeOptions"></BaseTag>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="应用类型" align="center" header-align="center" width="180">
        <template #default="scope">
          <BaseTag :value="scope.row.type" :dict="applicationTypeOptions"></BaseTag>
        </template>
      </el-table-column>
      <el-table-column prop="codeType" label="模板语言" align="center" header-align="center" width="180">
        <template #default="scope">
          <BaseTag :value="scope.row.codeType" :dict="codeTypeOptions"></BaseTag>
        </template>
      </el-table-column>
    </el-table>
  </BaseDialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import {DictDataModel} from "/@/api/system/model/DictModel";
import {useDictStore} from "/@/store/modules/dict";
import {listTemplate} from "/@/api/generator/TemplateApi";
import {TemplateModel, TemplateQueryModel} from "/@/api/generator/model/TemplateModel";
import {ElMessage} from "element-plus";
const isShow = ref(false)
const loading = ref(false)
const codeTypeOptions = ref<DictDataModel[]>([])
const applicationTypeOptions = ref<DictDataModel[]>([])
const templateTypeOptions = ref<DictDataModel[]>([])
const queryForm = reactive<TemplateQueryModel>({
  templateName: ''
})
const tableRef = ref()
const TemplateIds = ref([])
const data = ref<TemplateModel[]>([])
const emit = defineEmits(["load"])
const tableId = ref()
const flag = ref()
const init = async(tableIdValue: string,flagValue: string) => {
  tableId.value = tableIdValue
  flag.value = flagValue
  isShow.value = true
  loading.value = true
  await  dictLoad()
  let res = await listTemplate(queryForm,()=>{
    loading.value = false
  })
  data.value = res
}
const confirm = async () => {
  let res = tableRef.value.getSelectionRows() as TemplateModel[]
  if(res.length === 0){
    ElMessage.warning("模板不能为空")
    return;
  }
  let list = res.map(item => item.templateId)
  emit("load",list,tableId.value,flag.value)
  close()
}
const close = () => {
  TemplateIds.value = []
  isShow.value = false
}

const dictLoad = async () => {
  let dictStore = useDictStore();
  codeTypeOptions.value = await dictStore.getDict('code_type')
  applicationTypeOptions.value = await dictStore.getDict('application_type')
  templateTypeOptions.value = await dictStore.getDict('template_type')
}
defineExpose({init})
</script>

<style scoped>

</style>